import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Test
public void main(){
    //testGenJwt();
    //genJwt();
   // parseJwt();
}

@Test
public void testGenJwt() {
    Map<String, Object> claims = new HashMap<>();
    claims.put("id", 10);
    claims.put("username", "itheima");

    String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, "aXRjYXN0")
            .addClaims(claims)
            .setExpiration(new Date(System.currentTimeMillis() + 12 * 3600 * 1000))
            .compact();

    System.out.println(jwt);
}
@Test
public void genJwt(){
    Map<String, Object> claims = new HashMap<>();
    claims.put("id", 10);
    claims.put("username", "itheima");

    String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, "aXRjYXN0")
            .addClaims(claims)
            .setExpiration(new Date(System.currentTimeMillis() + 60 * 1000)) //有效期60s
            .compact();
    System.out.println(jwt);
    //输出结果：eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNjczMDA5NzU0fQ.RcVIR65AkGiax-ID6FjW60eLFH3tPTKdoK7UtE4A1ro
    //eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTAsInVzZXJuYW1lIjoiaXRoZWltYSIsImV4cCI6MTc1OTA0NTIyNX0.Ocmfpa0GrFJbcsmf97TgH-ETT4EemtTFR1ibZMlcUug
}

@Test
public void parseJwt(){
    Claims claims = Jwts.parser()
            .setSigningKey("aXRjYXN0")//指定签名密钥
            .parseClaimsJws("eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MTAsInVzZXJuYW1lIjoiaXRoZWltYSIsImV4cCI6MTc1OTA0NTIyNX0.Ocmfpa0GrFJbcsmf97TgH-ETT4EemtTFR1ibZMlcUug")
            .getBody();

    System.out.println(claims);
}